﻿using QInjection.Injector.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QInjection.Injector
{
    public class Configurator : IConfigurator
    {
        private Dictionary<Element, List<Tuple<Node, Branch>>> _map;

        public Configurator()
        {
            _map = new Dictionary<Element, List<Tuple<Node, Branch>>>();
        }

        private IEnumerable<Tuple<Node, Branch>> SafeGet(Element key)
        {
            List<Tuple<Node, Branch>> value = null;
            if (_map.TryGetValue(key, out value))
                return value;
            else
                return new List<Tuple<Node, Branch>>();
        }

        public void Add(Element key, Tuple<Node, Branch> item)
        {
            if (!_map.ContainsKey(key))
                _map[key] = new List<Tuple<Node, Branch>>();
            _map[key].Add(item);
        }

        public IEnumerable<Tuple<Node, Branch>> this[Element key]
        {
            get
            {
                return SafeGet(key);
            }
            set
            {
                _map[key] = value.ToList();
            }
        }

        public IEnumerable<Element> Elements
        {
            get
            {
                return _map.Keys.ToList();
            }
        }

        public IEnumerable<Tuple<Node, Branch>> Tuples(Element key)
        {
            return SafeGet(key);
        }
    }
}
